import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:wallpaper/components/images/image_hero.dart';

class ImageLoad extends StatelessWidget {
  final List imageUrls;
  final BoxFit fit;

  const ImageLoad(
      {super.key, required this.imageUrls, this.fit = BoxFit.cover});

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      itemCount: imageUrls.length,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        mainAxisSpacing: 10,
        crossAxisSpacing: 10,
        childAspectRatio: 1.5,
      ),
      itemBuilder: (context, index) {
        return buildHeroImage(context, index);
      },
    );
  }

  Widget buildHeroImage(BuildContext context, int index) {
    return Hero(
      tag: imageUrls[index],
      child: GestureDetector(
        onTap: () {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => ImageHero(
                        tagId: imageUrls[index],
                        imageUrls: imageUrls,
                        index: index,
                      )));
        },
        child: CachedNetworkImage(
          imageUrl: imageUrls[index],
          fit: fit,
          // progressIndicatorBuilder: (context, url, downloadProgress) {
          //   //显示loading效果
          //   return Center(
          //     child: SizedBox(
          //       width: 20,
          //       height: 20,
          //       child: CircularProgressIndicator(
          //         strokeWidth: 2,
          //       ),
          //     ),
          //   );
          // },
          errorWidget: (context, url, error) => Icon(
            Icons.perm_media,
            color: Theme.of(context).colorScheme.primaryContainer,
            size: 50,
          ),
        ),
      ),
    );
  }
}
